<template>
  <a-card :bordered="false">
    <div style="padding: 0px 40px">
      <a-steps :current="current">
        <a-step v-for="item in steps" :key="item.title" :title="item.title" />
      </a-steps>
      <div v-show="current == 0">
        <BasicSetting ref="basicSetting" @one="one" @oneData="oneData"></BasicSetting>
      </div>
      <div v-show="current == 1">
        <RewardSetting ref="rewardSetting" @two="two" @twoData="twoData"></RewardSetting>
      </div>
      <div v-show="current == 2">
        <SharingSetting ref="sharingSetting" @three="three" @threeData="threeData" @isSubmit="isSubmit"></SharingSetting>
      </div>
      <div class="steps-action">
        <a-button v-if="current > 0" style="margin-left: 8px" @click="prev"> 上一步 </a-button>
        <a-button v-if="current < steps.length - 1" type="primary" @click="next" style="margin-left: 8px"> 下一步 </a-button>
        <a-button v-if="current == steps.length - 1" type="primary" @click="submit" style="margin-left: 8px"> 保存 </a-button>
      </div>
    </div>
  </a-card>
</template>
<script>
import BasicSetting from '../stepView/basicSetting'
import RewardSetting from '../stepView/rewardSetting'
import SharingSetting from '../stepView/sharingSetting'
import { politeActivity, politeDetail, lvlGroup, politeUpdate, vipGroupId } from '@/api/marketingCenter/passenger.js'
export default {
  components: {
    BasicSetting,
    RewardSetting,
    SharingSetting
  },
  data() {
    return {
      id: this.$route.params.id,
      current: 0,
      oneList: null,
      twoList: [],
      form: {},
      threeList: null,
      isType: false,
      lvlGroup: '',
      vipGroupId: '',
      steps: [
        {
          title: '基础设置'
        },
        {
          title: '奖励设置'
        },
        {
          title: '分享设置'
        }
      ]
    }
  },
  mounted() {
    if (this.id != 'add') {
      this.detailList()
    }
  },
  methods: {
    one(val) {
      this.current = val
    },
    two(val) {
      this.current = val
    },
    three(val) {
      this.current = val
    },
    oneData(val) {
      this.oneList = val
    },
    twoData(val) {
      this.twoList = val
    },
    threeData(val) {
      this.threeList = val
    },
    isSubmit(val) {
      this.isType = val
    },
    next() {
      if (this.current == 0) {
        this.$refs.basicSetting.onSubmit()
        return
      }
      if (this.current == 1) {
        this.$refs.rewardSetting.onSubmit()
        return
      }
    },
    prev() {
      this.current--
    },
    postlvlGroup(list) {
      let params = {
        list: list
      }
      return lvlGroup(params).then(res => {
        if (res.code === 200) {
          return res.data
        } else {
          this.$message.error(res.msg)
          return res
        }
      })
    },
    postVipGroupId(list) {
      return vipGroupId(list).then(res => {
        if (res.code === 200) {
          console.log(res.data, ' res.data')
          return res.data
        } else {
          this.$message.error(res.msg)
          return res
        }
      })
    },
    interface(type) {
      let params = {
        id: this.id,
        type: type
      }
      return politeUpdate(params)
        .then(res => {
          if (res.code == 200) {
            return res.code
          } else {
            this.$message.success(res.msg)
          }
        })
        .catch(err => {
          resolve()
          console.log(err)
        })
    },

    async submit() {
      this.$refs.sharingSetting.onSubmit()
      if (!this.isType) return
      this.oneList.maxAward = this.twoList.maxAward
      // 集团卡等级
      if (this.oneList.selectedTags && this.oneList.selectedTags.length > 0) {
        this.oneList.lvlGroupId = await this.postlvlGroup(this.oneList.selectedTags)
      }
      // 人群包
      if (this.oneList.crowdList && this.oneList.crowdList.length > 0) {
        this.oneList.vipGroupId = await this.postVipGroupId(this.oneList.crowdList)
      }

      let result = null //  集团卡
      let res = null // 门店
      let sult = null // 人群包
      if (this.id && this.id != 'add' && this.oneList.isCheck == 'Y') {
        if (!this.oneList.maxAwardType) {
          result = await this.interface(1)
        }
        if (!this.oneList.storeGroupType) {
          res = await this.interface(2)
        }
        if (!this.oneList.crowdType) {
          sult = await this.interface(3)
        }
      }

      if (this.oneList.isCheck == 'N') {
        if (this.oneList.maxAwardType) {
          result = await this.interface(1)
          delete this.oneList.lvlGroupId
        }
        if (this.oneList.storeGroupType) {
          result = await this.interface(2)
          delete this.oneList.storeGroupId
        }
        if (this.oneList.crowdType) {
          sult = await this.interface(3)
          delete this.oneList.vipGroupId
        }
      }

      delete this.oneList.selectedTags
      delete this.oneList.crowdList
      let params = {
        basis: this.oneList,
        rewardList: this.twoList.arrList,
        share: this.threeList
      }
      if (this.id && this.id != 'add') {
        this.oneList.id = this.id
      }
      if (result == 200) {
        delete this.oneList.lvlGroupId
      }
      if (res == 200) {
        delete this.oneList.storeGroupId
      }
      if (sult == 200) {
        delete this.oneList.vipGroupId
      }

      console.log(params)

      await politeActivity(params).then(res => {
        if (res.code == 200) {
          this.$message.success('成功')

          this.$router.push('/marketingCenter/passenger/index')
        } else {
          this.current == 2
          this.$message.error(res.msg)
        }
      })
    },
    // 详情接口
    detailList() {
      politeDetail(this.id).then(res => {
        if (res.code == 200) {
          const data = res.data
          this.$refs.basicSetting.setData(data.basis) // 第一步
          let params = {
            rewardList: data.rewardList,
            maxAward: data.basis.maxAward
          }
          this.$refs.rewardSetting.setData(params) // 第二步
          this.$refs.sharingSetting.setData(data.share) // 第三步
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.steps-action {
  padding-left: 185px;
}
</style>